/*
 * Copyleft 2011 Power by Colen.
 *
 * Project: booking
 * Date: Jul 2, 2011
 */
package com.app.platform.base.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

/**
 * String Util Class
 *
 * @author Colen
 */
public final class StringUtil extends StringUtils {

    /** Private Constructor */
    private StringUtil() {
    }

    /**
     * To String
     *
     * @param obj Object.
     * @return string
     */
    public static String toStr(Object obj) {
        return (obj != null) ? obj.toString() : null;
    }

    /**
     * Is Trim Empty
     *
     * @param s String
     * @return true\false
     */
    public static boolean isTrimEmpty(String s) {
        return isEmpty(s) ? true : isEmpty(s.trim());
    }


    /**
     * 组装计算数据条数的SQL语句
     *
     * @param sql 传入原始SQL语句
     * @return 组装后的COUNT(*)语句
     */
    public static String cnvtConutSql(String sql) {
        if (isEmpty(sql)) {
            return null;
        }
        StringBuilder finalSql = new StringBuilder();
        finalSql.append("select count(*) c from (");
        finalSql.append(sql);
        finalSql.append(") temp");
        return finalSql.toString();
    }

    /**
     * Convert SQL to Page selector.
     *
     * @param sql SQL
     * @param curPage Current Page.
     * @param pageSize Page size
     * @return String SQL
     */
    public static String cnvtPageSql(String sql, int curPage, int pageSize) {
        // Empty sql
        if (isEmpty(sql)) {
            return sql;
        }

        // Start Row
        String fRow = String.valueOf(pageSize * (curPage - 1) + 1);
        // End Row
        String eRow = String.valueOf(pageSize * curPage);
        // OUT >>>
        return QUERY_TEMPLATE.replace("$P1$", sql).replace("$P2$", fRow)
                .replace("$P3$", eRow);
    }

    /** 分页查询模板 */
    private static final String QUERY_TEMPLATE = "SELECT * FROM (SELECT TB_.*, ROWNUM RN"
            + " FROM ($P1$) TB_ WHERE ROWNUM <= $P3$) WHERE RN >= $P2$";

    /**
     * Remove repeated Element.
     *
     * @param arr Source Array
     * @return string array
     */
    public static String[] removeRepeat(String[] arr) {
        if (CommonUtil.isEmpty(arr)) {
            return null;
        }
        Set<String> set = new LinkedHashSet<String>();
        for (String s : arr) {
            set.add(s);
        }
        // OUT
        return set.toArray(new String[0]);
    }

    /**
     * Make a String by String array
     *
     * @param arr Array.
     * @param spliter Split
     * @return
     */
    public static String makeString(String[] arr, String spliter) {
        StringBuilder bud = new StringBuilder();
        for (String a : arr) {
            bud.append(a).append(spliter);
        }
        return bud.substring(0, bud.length() - spliter.length());
    }

    /**
     * SubStr.
     *
     * @param s
     * @param len
     * @return
     */
    public static String substr(String s, int len) {
        if (isEmpty(s) || s.length() <= len) {
            return s;
        }
        return s.substring(0, len);
    }
    
    /**
     * cookie存储中文时，可先把存储的中文用utf-8编码一下
     * @param str
     */
    public static String encodeByUTF8(String str) {
    	try {
			str = URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {}
    	return str;
    }
    
}
